package com.proyecto.track.bo;

import java.util.*;

import javax.persistence.*;

@Entity
public class UserPerson {
	
	private Long id;
	private UserType userType;
	private Account account;
	private String mail;
	private String name;
	private String lastName;
	private List<ProjectUser> projectUsers = new ArrayList<ProjectUser>();
	private List<UserTask> userTasks = new ArrayList<UserTask>();
	private List<Coment> comments = new ArrayList<Coment>();
	
	public UserPerson(String mail, String name, String lastName){
		this.mail = mail;
		this.name = name;
		this.lastName = lastName;		
	}
	
	public UserPerson() {
	}

	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@ManyToOne
	public UserType getUserType() {
		return userType;
	}
	public void setUserType(UserType userType) {
		this.userType = userType;
	}

	public String getMail() {
		return mail;
	}
	public void setMail(String mail) {
		this.mail = mail;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	@OneToMany(mappedBy = "userPerson")
	public List<UserTask> getUserTasks() {
		return userTasks;
	}
	public void setUserTasks(List<UserTask> userTasks) {
		this.userTasks = userTasks;
	}
	
	@OneToMany(mappedBy = "userPerson")
	public List<ProjectUser> getProjectUsers() {
		return projectUsers;
	}
	public void setProjectUsers(List<ProjectUser> projectUsers) {
		this.projectUsers = projectUsers;
	}
	
	@OneToOne(cascade={CascadeType.ALL})
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}
	
	@OneToMany(mappedBy = "userPerson", cascade={CascadeType.ALL})
	public List<Coment> getComments() {
		return comments;
	}
	public void setComments(List<Coment> comments) {
		this.comments = comments;
	}


	public ProjectUser getProjectUsersById(long idProject) {
		for (ProjectUser projectUser : this.projectUsers) {
			if (projectUser.getProject().getId() == idProject) {
				return projectUser;
			}
			
		}
		
		return null;
	}

	public boolean userPersonIsInProject(Long idProject) {
		
		for (ProjectUser projectUser : this.projectUsers) {
			if (projectUser.getProject().getId() == idProject) {
				return true;
			}
		}
		return false;
	}	
	
	public UserTask getUserTaskById(long idTask) {
		for (UserTask userTask : this.userTasks) {
			if (userTask.getTask().getId() == idTask) {
				return userTask;
			}
		}
		return null;
	}
	
	public boolean userPersonIsInTask(Long idTask) {
		
		for (UserTask userTask : this.userTasks) {
			if (userTask.getTask().getId() == idTask) {
				return true;
			}
		}
		return false;
	}


}
